<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%String path = request.getContextPath() ;%>
<%@ taglib uri="/WEB-INF/tld/c.tld" prefix="c" %> 
<%@ taglib uri="/WEB-INF/tld/ui.tld" prefix="ui" %> 
<!DOCTYPE html>
<html lang="zh-CN" >
<head>
<c:set var="ctx" value="${pageContext.request.contextPath}" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>用户管理</title>
<jsp:include page="../../import.jsp" flush="true" />
</head>

<body class="iframeBody">
<div class="Wrapper">
<div id="pageInTab" class="pageInTab">
<div tabid="tabA" title="系统用户" lselected="true">
<div class="searchArea">
    <form id="searchBar">
        <div class="fieldset1 clearFix">
             <div class="fieldBox">
                <div class="label">用户名</div>
                <div class="field">
                    <input name="usrNm" id="usrNm" type="text" class="ui-textbox" value="">
                </div>
            </div>
            <div class="fieldBox">
                <div class="label">真实名称</div>
                <div class="field">
                    <input name="rlNm" id="rlNm" type="text" class="ui-textbox" value="">
                </div>
            </div>
            <div class="fieldBox">
                <div class="label">状态</div>
                <div class="field">
                    <select name="isValid" id="isValid">
                        <option value=" " seleted>请选择</option>
                        <c:forEach items="${isValidList }" var="isValid">
                        	<option value="${isValid.key }">${isValid.value }</option>
                        </c:forEach>
                    </select>
                </div>
            </div>
            
            <div class="fieldBox col_50">
                <div class="label">所属系统</div>
                <div class="field">
                    <select name="searchSysCd" id="searhcSysCdId">
                        <c:forEach items="${sysCdCdList }" var="sysCdTemp">
                        	<option value="${sysCdTemp.key }">${sysCdTemp.value }</option>
                        </c:forEach>
                    </select>
                </div>
            </div>
          
            <div class="clearFix"></div>
            <div class="btnsBox">
                <span id="searchBtn" class="sq-button sq-button-round">查询</span>
                <span id="addBtn" class="sq-button sq-button-round">新增</span>
                <span id="changeBtn" class="sq-button sq-button-round">修改</span>
                <span id="reSetPwdBtn" class="sq-button sq-button-round">密码重置</span>
            </div>
        </div>
    </form>
</div>

<div id="ligergrid1" class="sq-grid"></div>
<div id="addUsr_Html" style="display:none">
    <form id="addUsr_form">
    	 <input name="idS" id="idS" type="text" style="display: none;" value=""/>
        <div class="fieldset1 tabel_col tabel_col_s noBorder clearFix">
            
            <div class="fieldBox col_50">
                <div class="label">用户名</div>
                <div class="field">
                    <input name="usrNm" id="usrNmAdd" type="text" class="ui-textbox" value="" validate="{required:true}">
                    <span class="requiredSymbol">*</span>
                </div>
            </div>
            <div class="fieldBox col_50">
                <div class="label">真实名称</div>
                <div class="field">
                    <input name="rlNm" id="rlNmAdd" type="text" class="ui-textbox" value="" validate="{required:true}">
                    <span class="requiredSymbol">*</span>
                </div>
            </div>
            
            <div class="fieldBox col_50">
                <div class="label">手机号</div>
                <div class="field">
                    <input name="mp" id="mp" type="text" class="ui-textbox" value="" validate="{required:true,mobile:true}">
                    <span class="requiredSymbol">*</span>
                </div>
            </div>
            <div class="fieldBox col_50">
                <div class="label">邮箱</div>
                <div class="field">
                    <input name="email" id="email" type="text" class="ui-textbox" value="" >
                    
                </div>
            </div>
            <div class="fieldBox col_50">
                <div class="label">QQ</div>
                <div class="field">
                    <input name="qq" id="qq" type="text" class="ui-textbox" value="">
                </div>
            </div>
            
            <div class="fieldBox col_50">
                <div class="label">所属系统</div>
                <div class="field">
                    <select name="sysCd" id="sysCdId"  validate="{required:true, select: true}">
                        <c:forEach items="${sysCdCdList }" var="sysCdTemp">
                        	<option value="${sysCdTemp.key }">${sysCdTemp.value }</option>
                        </c:forEach>
                    </select>
                </div>
            </div>
            
            <div class="fieldBox col_50">
                <div class="label">状态</div>
                <div class="field">
                    <select name="refIsValid" id="refIsValid">
                        <c:forEach items="${isValidList }" var="isValid">
                        	<option value="${isValid.key }">${isValid.value }</option>
                        </c:forEach>
                    </select>
                </div>
            </div>
        </div>
        <div class="btnsBox">
            <span id="saveNewUsr_Btn" class="sq-button sq-button-hasicon Hide"><i class="sqBtnIco_10"></i>保存</span>
            <span id="changeSave_Btn" class="sq-button sq-button-hasicon Hide"><i class="sqBtnIco_10"></i>修改</span>
            <span class="sq-button sq-button-hasicon l-dialog-btn-cancel Hide"><i class="sqBtnIco_14"></i>取消</span>
            <input id="resetBtn" type="reset" value="重置" class="l-button l-button-reset Hide"/>
        </div>
    </form>
</div>
<div id="infoUsr_Html" style="display:none">
    <form id="infoUsr_form">
        <div class="fieldset1 tabel_col tabel_col_s noBorder clearFix">
            
            <div class="fieldBox col_50">
                <div class="label">用户名</div>
                <div class="field">
                    <span id="usrNmInfo"></span>
                </div>
            </div>
            <div class="fieldBox col_50">
                <div class="label">真实名称</div>
                <div class="field">
                	<span id="refRlNmInfo"></span>
                </div>
            </div>
            
            <div class="fieldBox col_50">
                <div class="label">手机号</div>
                <div class="field">
                	<span id="mpInfo"></span>
                </div>
            </div>
            <div class="fieldBox col_50">
                <div class="label">邮箱</div>
                <div class="field">
                	<span id="emailInfo"></span>
                </div>
            </div>
            <div class="fieldBox col_50">
                <div class="label">QQ</div>
                <div class="field">
                	<span id="qqInfo"></span>
                </div>
            </div>
            <div class="fieldBox col_50">
                <div class="label">注册方式</div>
                <div class="field">
                    <span id="refRegTypCdInfo"></span>
                </div>
            </div>
            <div class="fieldBox col_50">
                <div class="label">注册时间</div>
                <div class="field">
                    <span id="refRegDtInfo"></span>
                </div>
            </div>
            <div class="fieldBox col_50">
                <div class="label">状态</div>
                <div class="field">
                    <span id="refIsValidInfo"></span>
                </div>
            </div>
        </div>
    </form>
</div>

<div id="infoSun_Html" style="display:none">
    <div id="ligergrid2" class="sq-grid"></div>
</div>

</div>
<div tabid="tabB" title="供应商用户" id="tabBId">
	<iframe frameborder="0" name="tabB" id="tabB" src=""></iframe>
</div>
</div>
</div>

<script type="text/javascript">
var pageInApp={
		flag:true
}; // to 下一级子窗口 接口
var pageInTab=null;
var loadedTabs=[]
var grid = null,
	Editable=true;
$(function () {
	initTab();
	
	grid =  $("#ligergrid1").ligerGrid({
    	url: '<%=path%>/system/sysUsr/initList.do',
    	method : 'get',
        columns: [
			{ display: 'ID', name: 'id', width: 100, enabledSort:false },
            { display: '用户名', name: 'usrNm', minWidth: 140, 
            	render: function (rowdata, rowindex, value, column) { 
        		var h = '';
    			if(value&&value!="null"){
    		    h+='<a href="javascript:detailBtn(';
                h+="'"+rowdata.id+"','"+rowdata.refSysCd+"'";
                h+=')">'+value+'</a>'
    			}
                return h;
    		},enabledSort:true},
            { display: '真实名称', name: 'refRlNm', minWidth: 140, enabledSort:true },
            { display: '联系方式', name: 'mp', minWidth: 140 , enabledSort:true},
            { display: '邮箱', name: 'email', minWidth: 140 , enabledSort:true},
            { display: '状态', name: 'refIsValidNm', minWidth: 40 , enabledSort:true},
            { display: '操作',
                isSort: false,
                width: 100,
                align:'left',
                render: function (rowdata, rowindex, value){
                	 var h="";
                    h+='<a class="sq-button-hasicon" href="javascript:detailInfo(';
                    h+="'"+rowdata.id+"','"+rowdata.usrNm+"'";
                    h+=')"><i class="sqBtnIco_2"></i>岗位信息</a>';
                    return h;
                }
            }
        ],
        pageSize:10,
        root:'rows',
        record:'total',
        hideLoadButton : true,
        width: '100%',
        isScroll: false,
        checkbox:true,
        rownumbers:true,
        onRenderComplete: function () {
        	iframeResize ();
        },
        onSelectRow: function (rowdata, rowid, rowobj) {
        	isEditable(rowdata.refIsSysDef);
        },
        onSuccess: function (data, grid) {
            if (data.rows.length==0) {
                top.$.ligerDialog.SQalert({content: LogUtils.LOG_SYS_ORG_USR_NOT_EXIST,SQ_type: "warning"});
            };
        }
    });

    $("#searchBar").ligerForm({
      
    });

    $("#pageloading").hide();
    $("#searchBtn").click(function(event) {
         var form = liger.get("searchBar");
         var data = form.getData();
         //alert(JSON.stringify(data));
         for (name in data){
            grid.setParm(name, $.trim(data[name]));
         };
        grid.reload();
    });
	
   
    $("#addBtn").click(function(event) {
    	if(liger.get("addUsr_form")){
    		var form=liger.get("addUsr_form");
    		$("#resetBtn").click();  //重置 input-text
            form.setData({  //重置表单
                sysCd:'11000',
                refIsValid:1
            });
    	}
        $.ligerDialog.open({
            width: 760,
            // top: 100,
            buttons: [
                {
                    text: "保存", cls: 'l-dialog-btn-ok', onclick:function(){
                    	$("#saveNewUsr_Btn").click();
                    }
                }, {
                    text: "取消", cls: 'l-dialog-btn-no', onclick:function(){
                    	$.ligerDialog.hide();
                    }
                }
            ],
            title : '新增用户',
            target: $("#addUsr_Html")
        });
        var ligerForm = $("#addUsr_form").ligerForm({validate: true});
        liger.get("sysCdId").setEnabled();
    });
    $("#saveNewUsr_Btn").click(function(event) {
        var form = liger.get("addUsr_form");
        if (!form.valid()) {
            top.$.ligerDialog.SQalert({content: LogUtils.LOG_CMM_MUST_FIX_ITEM_INFO,SQ_type: "warning"});
            return false; //验证不通过，不执行以下代码
        }
        var emailVal = document.getElementById("email").value;
        
        	
    	var regu = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    	var re = new RegExp(regu);
    	if (re.test(emailVal)) {
    		addUsrMethod(form);
   	    }else{
   	    	if(emailVal!=null&&emailVal!=""){
	   	    	$.ligerDialog.confirm('邮箱格式不正确!如果继续点击是', function (flag) {
	   	    		if(flag == false)return;
	   	    		addUsrMethod(form);
	   	    	});
   	    	}else{
   	    		addUsrMethod(form);
   	    	}
   	    }
    });
    
    $("#changeBtn").click(function(event) {
    	
    	var selectedRows = grid.getSelectedRows();
		if(selectedRows.length != 1){
			top.$.ligerDialog.SQalert({content: LogUtils.LOG_CMM_SELECT_ONLY_A_ROW,SQ_type: "warning"});
			return;
		}
		
		if(Editable==false){
    		top.$.ligerDialog.SQalert({content: LogUtils.LOG_SYS_ORG_INSIDE_FORBID_HANDLE,SQ_type: "warning"});
    		return;
    	}; 
		
		var _id = selectedRows[0].id;
		var _sysCd = selectedRows[0].refSysCd;
        $.ligerDialog.SQopen({
            width: 760,
            // top: 100,
             buttons: [
                {
                    text: "保存", cls: 'l-dialog-btn-ok', onclick:function(){
                    	$("#changeSave_Btn").click();
                    }
                }, {
                    text: "取消", cls: 'l-dialog-btn-no', onclick:function(){
                    	$.ligerDialog.hide();
                    }
                }
            ],
            title : '修改用户',
            target: $("#addUsr_Html")
        });
        //初始化数据
        initData(_id, _sysCd);
        //禁止编辑
        var ligerForm = $("#addUsr_form").ligerForm({validate: true});
        liger.get("sysCdId").setDisabled();
    });
    $("#changeSave_Btn").click(function(event) {
        var form = liger.get("addUsr_form");
        if (!form.valid()) {
            top.$.ligerDialog.SQalert({content: LogUtils.LOG_CMM_MUST_FIX_ITEM_INFO,SQ_type: "warning"});
            return false; //验证不通过，不执行以下代码
        }
        var data = form.getData();
        var waitingDialog=null;
         $.ajax({
             url: '<%=path%>/system/sysUsr/modify.do',
             type: 'post',
             dataType: 'json',
             data: data,
            beforeSend: function (msg) {
                 manager = $.ligerDialog.waitting(LogUtils.LOG_CMM_DATA_LOADING);
            },
             success: function (msg) {
                manager.close();
                if(msg.success == 1){
                	$.ligerDialog.hide();
                	top.$.ligerDialog.SQalert({content: msg.info,SQ_type: "success"});
					 grid.reload();
				 }else{
					 top.$.ligerDialog.SQalert({content: msg.info,SQ_type: "error"});
				 }
             }
         });
    });
    
    //密码重置
	$("#reSetPwdBtn").click(function(event) {
		var selectedRows = grid.getSelectedRows();
		if(selectedRows.length == 0){
			top.$.ligerDialog.SQalert({content: LogUtils.LOG_SYS_ORG_SELECT_RESET_DATA,SQ_type: "warning"});
			return;
		}
		
		var _ids = [];
		
		for ( var index in selectedRows) {
			_ids.push(selectedRows[index].id);
		}
		$.ligerDialog.confirm(LogUtils.LOG_ORD_RESET_CONFIRM, function (flag) {
			if(flag == false)return;
			$.ajax({
				type: 'post',
				dataType:'json',
				data:{ids:JSON.stringify(_ids)},
				url: '<%=path%>/system/sysUsr/reSetPwd.do',
				success:function(msg){
					 if(msg.success == 1){
						 top.$.ligerDialog.SQalert({content: msg.info,SQ_type: "success"});
						 grid.reload();
					 }else{
						 top.$.ligerDialog.SQalert({content: msg.info,SQ_type: "error"});
					 }
				}
			});
		});
	});
    $("#Back_Btn").click(function(event) {
        
    });

});

//初始化Tab
function initTab(){
	 pageInTab = $("#pageInTab").ligerTab({
         height: 'auto',
         contextmenu: false,
         minIframeHeight: GET_minIframeHeight(),
         onBeforeSelectTabItem: function (tabid) {
         	if(tabid!="tabA"){
	            	
         	}
         },
         onAfterSelectTabItem: function(tabid) {
             if (loadedTabs.indexOf(tabid)<0) {
                 loadedTabs.push(tabid);
                 switch (tabid){
                     case "tabB":
                         pageInTab.setTabItemSrc(tabid, "<%=path%>/system/sysUsr/toSplUsrListPage.do");
                         break;
                 }
             };
             SetIframe_height();
         }
     });

     $.extend(true, pageInApp, {
         pageInTab: pageInTab
     });
};

function detailBtn(_id,_sysCd) {
	$("#changeSave_Btn").hide();
	$("#saveNewUsr_Btn").hide();
    $.ligerDialog.open({
        width: 760,
        // top: 100,
        buttons: [
                {
                    text: "关闭", cls: 'l-dialog-btn-no', onclick:function(){
                    	$.ligerDialog.hide();
                    }
                }
            ],
        title : '用户详情',
        target: $("#infoUsr_Html")
    });
  //初始化数据
    initInfoData(_id, _sysCd);
    var ligerForm = $("#infoUsr_form").ligerForm({validate: true});
}

function detailInfo(id,usrNm){
    $.ligerDialog.open({
        width: 760,
        // top: 100,
        buttons: [
                {
                    text: "关闭", cls: 'l-dialog-btn-no', onclick:function(){
                    	$.ligerDialog.hide();
                    }
                }
            ],
        title : '岗位详情',
        target: $("#infoSun_Html")
    });
  //初始化数据
    initInfoSunData(id,usrNm);
}
/*
是否系统内置参数获取
*/
function isEditable(isSysDef){
	if(isSysDef==1){
		Editable=false;
	}else{
		Editable=true;
	}
}
function iframeResize_pageIn () {
    if (parent.pageInApp) {
        parent.pageInApp.pageInTab.onResize();
        iframeResize ()
    };
}
function iframeResize () {
    if (parent.parent.mainApp) {
        parent.parent.mainApp.parentTab.onResize();
    };
}
function initInfoSunData(id,usrNm){
	$("#ligergrid2").ligerGrid({
    	url: '<%=path%>/system/sysUsrFunAuthR/getByCondition.do?id='+id+'&usrNm='+usrNm,
    	method : 'get',
        columns: [
            { display: '用户名', name: 'usrNm', minWidth: 140, enabledSort:true },
            { display: '角色名', name: 'roleNm', minWidth: 140 , enabledSort:true},
            { display: '组织架构', name: 'orgDispNm', minWidth: 140 , enabledSort:true}
        ],
        usePager: false, 
        pageSize:10,
        root:'rows',
        record:'total',
        hideLoadButton : true,
        width: '100%',
        height: 300,
        isScroll: true,
        checkbox:false,
        rownumbers:true,
        onRenderComplete: function () {
        	iframeResize ();
        },
        onSelectRow: function (rowdata, rowid, rowobj) {
        },
        onSuccess: function (data, grid) {
        }
    });
}
function initData(_id, _sysCd){
	//alert(_id);
	if(!_id)return;
	var _url = '<%=path%>/system/sysUsr/getById.do?id='+_id;
	$.ajax({
		type: 'get',
		dataType:'json',
		data:{id:_id, sysCd:_sysCd},
		url: _url,
		success:function(msg){
			 if(msg.success == 1){
				 setData(msg.data);//设置数据
			 }else{
				 top.$.ligerDialog.SQalert({content: msg.info,SQ_type: "error"});
			 }
		}
	});
}
function initInfoData(_id, _sysCd){
	//alert(_id);
	if(!_id)return;
	var _url = '<%=path%>/system/sysUsr/getById.do?';
	$.ajax({
		type: 'get',
		dataType:'json',
		data:{id:_id, sysCd:_sysCd},
		url: _url,
		success:function(msg){
			 if(msg.success == 1){
				 setInfoData(msg.data);//设置数据
			 }else{
				 top.$.ligerDialog.SQalert({content: msg.info,SQ_type: "error"});
			 }
		}
	});
}
//设置页面数据
function setData(data){
	
	var form = new liger.get("addUsr_form");
	form.setData({
		usrTypCd:data.usrTypCd,
		sysCd:data.refSysCd,
		refIsValid:data.refIsValid
	});
	$("#idS").val(data.id);
	$("#usrNmAdd").val(data.usrNm);
	$("#rlNmAdd").val(data.refRlNm);
	$("#mp").val(data.mp);
	$("#email").val(data.email);
	$("#openId").val(data.openId);
	$("#qq").val(data.qq);
	
	//$("input[name=genderCd][value='"+data.genderCd+"']").attr("checked",true);
	
}
//设置页面数据
function setInfoData(data){
	
	var form = new liger.get("infoUsr_form");
	
	$("#usrNmInfo").text(isNulltoNBSP(data.usrNm));
	$("#refRlNmInfo").text(isNulltoNBSP(data.refRlNm));
	$("#mpInfo").text(isNulltoNBSP(data.mp));
	$("#emailInfo").text(isNulltoNBSP(data.email));
	$("#openIdInfo").text(isNulltoNBSP(data.openId));
	$("#qqInfo").text(isNulltoNBSP(data.qq));
	$("#refRegTypCdInfo").text(isNulltoNBSP(data.refRegTypCdNm));
	$("#refRegDtInfo").text(isNulltoNBSP(DateUtils.formatDate(new Date(data.refRegDt), DateUtils.YYYY_MM_DDHHMMSS)));
	$("#refIsValidInfo").text(isNulltoNBSP(data.refIsValidNm));
	
}
function isNulltoNBSP(a){
	if(a==null || a==undefined){
		return ""
	}else{
		return a;
	}
}

function addUsrMethod(form){
	
	var data = form.getData();
    var genderCd = $('input[name="genderCd"]:checked ').val();
    data["genderCd"] = genderCd;
    var waitingDialog=null;

     $.ajax({
         url: '<%=path%>/system/sysUsr/add.do',
         type: 'post',
         dataType: 'json',
         data: data,
        beforeSend: function (argument) {
             manager = $.ligerDialog.waitting(LogUtils.LOG_CMM_DATA_LOADING);
        },
         success: function (msg) {
            manager.close();
            if(msg.success == 1){
            	top.$.ligerDialog.SQalert({content: msg.info,SQ_type: "success"});
				 grid.reload();
				 $.ligerDialog.hide();
			}else{
				top.$.ligerDialog.SQalert({content: msg.info,SQ_type: "error"});
			}
         }
     });
}
</script>
</body>
</html>
